package cn.com.twoke.application.game.mario.resource.texture;

import cn.com.twoke.application.game.mario.core.ecs.component.PositionComponent;
import cn.com.twoke.application.game.mario.core.ecs.entity.Entity;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

@Slf4j
public class TextureFactory {

    @Builder
    @Getter
    public static class RenderContext {
        private Entity entity;
        private PositionComponent position;
        private Supplier<String> render;
        private Graphics2D graphics;
    }


    private static final Map<String, Consumer<RenderContext>> renderPool = new HashMap<>();


    public static void register(String key, Consumer<RenderContext> consumer) {
        if (renderPool.containsKey(key)) {
            return;
        }
        renderPool.put(key, consumer);
    }


    public static void render(Entity entity, PositionComponent position, Supplier<String> render, Graphics2D g2d) {
        Consumer<RenderContext> renderContextConsumer = renderPool.get(render.get());
        if (Objects.nonNull(renderContextConsumer)) {
            renderContextConsumer.accept(RenderContext.builder()
                            .graphics(g2d)
                            .render(render)
                            .entity(entity)
                            .position(position)
                    .build());
        } else {
            log.warn("纹理绘制函数 {} 为空", render.get());
        }
    }

}
